﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.IO;
using Microsoft.SharePoint.WebControls;

namespace Macaw.Moss2007.RobotsTxtSolution
{
    public class RobotsTxtModify : LayoutsPageBase
    {
        protected TextBox tbContent;
        protected Label lblLastModified;
        protected Label lblLastModifiedBy;

        protected override void OnLoad(EventArgs e)
        {
            

            if (!IsPostBack)
            {
                Populate();   
            }
            base.OnLoad(e);
        }

        private void Populate()
        {
            try
            {
                tbContent.Text = SPContext.Current.Site.RootWeb.GetFileAsString("Robots.txt");
                SPFile file = SPContext.Current.Site.RootWeb.GetFile("Robots.txt");
                lblLastModified.Text = file.TimeLastModified.ToString();
                lblLastModifiedBy.Text = file.ModifiedBy.Name;
            }
            catch
            {
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            MemoryStream ms = new MemoryStream();
            StreamWriter sw = new StreamWriter(ms);
            sw.Write(tbContent.Text);
            sw.Flush();
            ms.Seek(0, SeekOrigin.Begin);
            SPFile file = SPContext.Current.Site.RootWeb.RootFolder.Files.Add("Robots.txt", ms , true);
            Populate();
        }
    }
}
